package com.cictec.middleware.tde.rm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.cictec.middleware.commons.utils.PMSUtils;

public class MinaTcpClientMutil {
	
	private static String resource = "test.properties";
	private static Properties prop = null;

	public static void main(String[] args) throws Exception {
		prop = getProperties();
		
		String loadClientCycle = prop.getProperty("loadClientCycle");
		long clientCycle = 6000L;
		if (!PMSUtils.isEmpty(loadClientCycle)) {
			clientCycle = Long.parseLong(loadClientCycle);
		}

		String dataFilePath = prop.getProperty("dataFilePath");

		List<String> list = readfile(dataFilePath);

		int index = 1;
		for (String filepath : list) {
			List<String> fileData = new ArrayList<String>();
			fileData = readTxtFile(filepath);

			Thread.sleep(clientCycle);
			muiltSend("【client-"+index+"】", fileData);
			index ++;
		}
	}

	public static void muiltSend(String clientId, List<String> dataList) {
		
		new Thread() {
			public void run() {
				try {
					MinaClient client = new MinaClient(clientId);
					client.send(dataList);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	
	public static List<String> readfile(String filepath) throws FileNotFoundException, IOException {
		List<String> list = new ArrayList<String>();
		try {
			File file = new File(filepath);
			if (!file.isDirectory()) {
				list.add(file.getPath());
			} else if (file.isDirectory()) {
				System.out.println("文件夹");
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + "\\" + filelist[i]);
					if (!readfile.isDirectory()) {
						list.add(readfile.getPath());
					} else if (readfile.isDirectory()) {
						List<String> subList = readfile(filepath + "\\" + filelist[i]);
						list.addAll(subList);
					}
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("readfile()   Exception:" + e.getMessage());
		}
		return list;
	}
	
	public static List<String> readTxtFile(String filePath) {
		List<String> result = new ArrayList<String>();
		try {
			String encoding = "GBK";
			File file = new File(filePath);
			if ((file.isFile()) && (file.exists())) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					result.add(lineTxt);
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return result;
	}
	
	public static Properties getProperties() {
		if (prop == null) {
			prop = new Properties();
			InputStream is = MinaTcpClientMutil.class.getClassLoader().getResourceAsStream(resource);
			try {
				prop.load(is);
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
		return prop;
	}
}